﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ComicViewer.IManhua;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace ComicViewer
{
    /// <summary>
    /// Interaction logic for ViewWindow.xaml
    /// </summary>
    public partial class ViewWindow : Window
    {
        bool m_isMoving = false;
        Point m_startMovePos;
        Point m_imageStartPos;

        Storyboard sb;
        EasingDoubleKeyFrame leftFrame;
        EasingDoubleKeyFrame topFrame;
        long dTick;
        public ViewWindow()
        {
            InitializeComponent();


            //NameScope.SetNameScope(this, new NameScope());
            //this.RegisterName(ListViewPaper.Name, ListViewPaper);



            sb = new Storyboard();

            DoubleAnimationUsingKeyFrames frames = new DoubleAnimationUsingKeyFrames();
            Storyboard.SetTargetName(frames, ListViewPaper.Name);
            Storyboard.SetTargetProperty(frames, new PropertyPath(Canvas.LeftProperty));

            leftFrame = new EasingDoubleKeyFrame(0);
            leftFrame.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0,200));
            frames.KeyFrames.Add(leftFrame);
            sb.Children.Add(frames);


            DoubleAnimationUsingKeyFrames topframes = new DoubleAnimationUsingKeyFrames();
            Storyboard.SetTargetName(topframes, ListViewPaper.Name);
            Storyboard.SetTargetProperty(topframes, new PropertyPath(Canvas.TopProperty));

            topFrame = new EasingDoubleKeyFrame(0);
            topFrame.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 200));
            topframes.KeyFrames.Add(topFrame);
            sb.Children.Add(topframes);
            sb.Completed += new EventHandler(sb_Completed);
            this.Resources.Add("SBSB", sb);
        }

        void sb_Completed(object sender, EventArgs e)
        {
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Key == Key.Escape)
            {
                DialogResult = true;
            }
        }

        protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseRightButtonUp(e);

            DialogResult = true;
        }

        public void SetPage(ComicTable table)
        {
            ListViewPaper.ItemsSource = table.Pages;
            Canvas.SetTop(ListViewPaper, 0);
            Canvas.SetLeft(ListViewPaper, 0);
        }

        private void GridMask_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            m_imageStartPos = new Point(
                Canvas.GetLeft(ListViewPaper),
                Canvas.GetTop(ListViewPaper));
            m_startMovePos = e.GetPosition(GridMask);
            dTick = DateTime.Now.Ticks;
            Cursor = Cursors.Hand;
            m_isMoving = true;
        }

        private void GridMask_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //if (e.RightButton == MouseButtonState.Released)
            //{
                if (m_isMoving)
                {
                    Point currentPos = e.GetPosition(GridMask);
                    /*<Storyboard x:Key="Storyboard1">
                <Int32AnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Row)" Storyboard.TargetName="listView">
                    <EasingInt32KeyFrame KeyTime="0:0:0.3" Value="1"/>
                    <EasingInt32KeyFrame KeyTime="0:0:0.7" Value="3"/>
                </Int32AnimationUsingKeyFrames>
            </Storyboard>*/

                    //leftFrame.Value = Canvas.GetLeft(ListViewPaper) + (currentPos.X - m_startMovePos.X) * 0.2;
                    //topFrame.Value = Canvas.GetTop(ListViewPaper) + (currentPos.Y - m_startMovePos.Y) * 0.2;
                    //leftFrame.KeyTime = KeyTime.FromTimeSpan(new TimeSpan((long)((DateTime.Now.Ticks - dTick) * 0.5)));
                    //topFrame.KeyTime = leftFrame.KeyTime;
                    //sb.Begin();
                }
                m_isMoving = false;
                Cursor = Cursors.Arrow;
            //}

        }

        private void GridMask_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_isMoving)
            {
                sb.Stop();
                Point currentPos = e.GetPosition(GridMask);
                Canvas.SetTop(ListViewPaper, m_imageStartPos.Y + currentPos.Y - m_startMovePos.Y);
                Canvas.SetLeft(ListViewPaper, m_imageStartPos.X + currentPos.X - m_startMovePos.X);
            }
        }
    }
}
